//
//  SearchViewController.m
//  礼物说
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "SearchViewController.h"

#define kSEARCHURL @"http://api.liwushuo.com/v2/search/hot_words"

@interface SearchViewController ()<UISearchResultsUpdating, UISearchBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) BaseSearchController *searchController;
@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, retain) BaseLabel *label;

@end

@implementation SearchViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc {
    [_collectionView release];
    [_dataArr release];
    [_label release];
    [_searchController release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    [GiFHUD show];
    [self createLable];
    [self dataHandel];
    [self createSearch];
}

#pragma mark - 数据解析
- (void)dataHandel {
    self.dataArr = [NSMutableArray array];
    
    [XRAFNetworkTool GETUrl:kSEARCHURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        self.dataArr = responseObject[@"data"][@"hot_words"];
        
        [GiFHUD dismiss];
        [self createCollection];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma mark - 创建CreateLabel
- (void)createLable {
    self.label = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHTS(40))];
    _label.text = @"大家都在搜索";
    _label.textColor = [UIColor grayColor];
    _label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];
    [_label release];
}

#pragma mark - 创建collectionView
- (void)createCollection {
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake((kWIDTH - kWIDTHS(50)) / 4, kWIDTHS(40));
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, kHEIGHTS(40), kWIDTH, kWIDTHS(40) * ((_dataArr.count + 3) / 4) + 10) collectionViewLayout:layout];
//    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.str = _dataArr[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NewSearchViewController *newSearchVc = [[NewSearchViewController alloc] init];
    NSString *word = [_dataArr[indexPath.row] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
}

#pragma mark - 创建Search
- (void)createSearch {
    
    /** 创建searchController*/
    self.searchController = [[BaseSearchController alloc] initWithSearchResultsController:nil];
    /** 签代理*/
    _searchController.searchResultsUpdater = self;
    _searchController.searchBar.delegate = self;
    /** 设置开始搜索时是否背景显示*/
    _searchController.dimsBackgroundDuringPresentation = NO;
    /** 设置开始搜索是否隐藏导航栏*/
    _searchController.hidesNavigationBarDuringPresentation = NO;
    /** 设置搜索的大小*/
    _searchController.searchBar.frame = CGRectMake(kWIDTHS(10), kHEIGHTS(5), kWIDTHS(280), kHEIGHTS(40));
    self.navigationItem.titleView = self.searchController.searchBar;
    /** 释放*/
    [_searchController release];
    /** 设置搜索的提示字*/
    _searchController.searchBar.placeholder = @"快选一份礼物，送给TA吧";
    /** 设置输入法的样式*/
    _searchController.searchBar.keyboardType = UIKeyboardAppearanceDefault;
    /** 搜索进入活跃状态*/
    [_searchController.searchBar setShowsCancelButton:YES animated:YES];
}

/** 获取所输入的文字*/
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
}

/** 点击搜索 触发的方法*/
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    /** 取消搜索的活动状态*/
    [searchBar setShowsCancelButton:NO animated:YES];
    /**取消键盘的响应 */
    [searchBar resignFirstResponder];
    
    /** 跳转*/
    NewSearchViewController * newSearchVc = [[NewSearchViewController alloc] init];
    NSString * word = [searchBar.text stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    NSLog(@"%@", word);
    newSearchVc.words = word;
    newSearchVc.str = searchBar.text;
    [self.navigationController pushViewController:newSearchVc animated:YES];
    [newSearchVc release];
}

//取消按钮的点击方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
